package com.kstudio.videocapture.listimage;

import java.io.File;
import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.kstudio.videocapture.KVideoCapture;
import com.kstudio.videocapture.R;
import com.kstudio.videocapture.editimage.KImageEditViewGL;
import com.kstudio.videocapture.util.KLog;
import com.kstudio.videocapture.z.KFileListAsync;
import com.kstudio.videocapture.z.KFileListAsync.KVideoCaptureListener;

public class KImageListActivity extends Activity implements KVideoCaptureListener {
    private final KLog log = new KLog("KImageList");

    public static final int IMAGELIST_MODE_GRID    = 0x0001;
    public static final int IMAGELIST_MODE_SUBLIST = 0x0002;

    private Context mContext;
    private int     mImageListMode;
    private String  mPath;

    private KImageListLoadBitmap mLoadBitmap;
    private int mBmWidth;
    private int mBmHeight;

    private ArrayList<KImageListItem> mImageList;

    private GridView mGrid;
    private KImageListAdapter mAdapter;
    private int mGridWidth;
    private int mGridHeight;
    private int mIconWidth;
    private int mIconHeight;

    private KImageListViewSubListGL mSubList;

    private KFileListAsync mSnapShotAsync;

    private int mImageEditorListSize;
    private int mBaseIconWidth;
    private int mBaseIconHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        log.i("***************** onCreate *****************");
        super.onCreate(savedInstanceState);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        switch(metrics.densityDpi) {
            case DisplayMetrics.DENSITY_MEDIUM:
                mBmWidth  = 72;
                mBmHeight = 72;
                break;

            case DisplayMetrics.DENSITY_HIGH:
                mBmWidth  = 108;
                mBmHeight = 108;
                break;

            case DisplayMetrics.DENSITY_XHIGH:
                mBmWidth  = 144;
                mBmHeight = 144;
                break;

            case DisplayMetrics.DENSITY_XXHIGH:
                mBmWidth  = 216;
                mBmHeight = 216;
                break;

            default:
                break;
        }

        Intent intent = getIntent();
        if(intent != null) {
            mImageListMode = intent.getIntExtra("ImageListMode", IMAGELIST_MODE_GRID);
        } else {
            mImageListMode = IMAGELIST_MODE_GRID;
        }

        init_activity();

        if(mImageListMode == IMAGELIST_MODE_GRID) {
            init_grid();
        } else if(mImageListMode == IMAGELIST_MODE_SUBLIST) {


        }
    }

    @Override
    protected void onRestart() {
        log.i("***************** onRestart *****************");
        super.onRestart();
    }

    @Override
    protected void onStart() {
        log.i("***************** onStart *****************");
        super.onStart();
    }

    @Override
    protected void onResume() {
        log.i("***************** onResume *****************");
        super.onResume();
    }

    @Override
    protected void onPause() {
        log.i("***************** onPause *****************");
        super.onPause();
    }

    @Override
    protected void onStop() {
        log.i("***************** onStop *****************");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        log.i("***************** onDestroy *****************");
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_imagelist, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.menu_mode_hori :
                mImageListMode = IMAGELIST_MODE_SUBLIST;
                return true;
            case R.id.menu_trash :
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onProgressUpdate() {
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onResult(int result) {
    }

    @Override
    public void onProgressUpdate(String path, Bitmap b) {}

    private class KOnItemClickListenr implements OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> adapterview, View view, int i, long l) {
            log.i("onItemClick %d",i);

//            ArrayList<KImageEditItem> mIntentList = new ArrayList<KImageEditItem>();

            int StartPos = KImageEditViewGL.computeLinearListStartPos(i, mImageList.size(), mImageEditorListSize);

            int cnt = 0;
//            for(KImageListItem item : mImageList) {
//                KImageListItem intentItem = null;
//                if(cnt >= StartPos && cnt < StartPos + mImageEditorListSize) {
//                    Bitmap bm = item.getBitmap();
//                    if(bm == null) {
//                        intentItem = KImageEditItem.create(item.getPath());
//                    } else {
//                        if(bm.getWidth() > mBaseIconWidth || bm.getHeight() > mBaseIconHeight ) {
//                            intentItem = KImageEditItem.create(item.getPath(),
//                                                               Bitmap.createScaledBitmap(bm, mBaseIconWidth, mBaseIconHeight, false));
//
//                        } else {
//                            intentItem = KImageEditItem.create(item.getPath(), bm);
//                        }
//                    }
//                } else {
//                    intentItem = KImageEditItem.create(item.getPath());
//                }
//                mIntentList.add(intentItem);
//                cnt++;
//            }
//
//            Intent intent = new Intent(mContext, KImageEditActivity.class);
//            intent.putExtra("saveddata", mIntentList);
//            intent.putExtra("index", i);
//            startActivity(intent);
        }
    }

    /* private */
    private void init_activity() {
        mContext = this;

        KVideoCapture application = ((KVideoCapture) getApplicationContext());
        mPath = application.getStoragePath();
        mLoadBitmap = new KImageListLoadBitmap(mContext, "KImageListThumbnail", 10 *3, Integer.MAX_VALUE);
    }

    private void init_grid() {
        setContentView(R.layout.image_list_activity);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

        mGrid = (GridView) findViewById(R.id.image_grid);
        KImageListViewAlbumIcon albumicon = new KImageListViewAlbumIcon(mContext);
        mGridWidth = albumicon.getAlbumWidth();
        mGridHeight = albumicon.getAlbumHeight();
        mIconWidth = mGridWidth - mGridWidth / 5;
        mIconHeight = mGridHeight - mGridHeight / 5;
        mGrid.setColumnWidth(mGridWidth);

        mImageList = new ArrayList<KImageListItem>();
        mAdapter = new KImageListAdapter(this, mLoadBitmap, mImageList, mGridWidth, mGridHeight);

        updateFileList(mPath);
        mGrid.setAdapter(mAdapter);
        mGrid.setOnItemClickListener(new KOnItemClickListenr());
    }

    private void init_sublist() {
        setContentView(R.layout.image_edit_activity);

        mSubList = (KImageListViewSubListGL) findViewById(R.id.image_edit_view_gl);
        mImageList = new ArrayList<KImageListItem>();
        updateFileList(mPath);

        mSubList.setInit(mImageList, 0);
    }

    private void updateFileList(String path) {
        File[] files = new File(path).listFiles();
        String name;
        String extention;
        int extentionIndex;
        String[] PictureExtiontion = {"PNG","png","JPG","jpg","BMP","bmp"};

        mImageList.clear();

        for(File file: files) {
            name = file.getPath();
            extentionIndex = name.lastIndexOf(String.valueOf('.'));
            if(extentionIndex > 0) {
                extention = name.substring(extentionIndex + 1);
                for(String ex : PictureExtiontion) {
                    if(extention.equals(ex)) {
                        mImageList.add(new KImageListItem(file.getPath()));
                        break;
                    }
                }
            }
        }
    }
}
